package com.jfee.dds.dbmgr;

import com.jfee.dds.constants.CommonConstants;
import com.jfee.dds.model.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 项目数据库管理。提供根据项目编码查询数据库名称和IP的接口。
 *
 * @author sf.xiong
 * @version 2019-05-31
 */
@Component
public class TenantDBMgr {

    @Autowired
    private HashOperations<String, String, Object> hashOperations;

    // 实际开发中改为从缓存获取
    public synchronized Db getDataProperties(String tenantId) {
        Map<String, Object> tenantInfoMap = hashOperations.entries(CommonConstants.RDS_TENANT_INFO_KEY);
        if (!tenantInfoMap.containsKey(tenantId)) {
            throw new RuntimeException("无法读取租户配置");
        }
        Db db = (Db) tenantInfoMap.get(tenantId);
        if (db == null) {
            throw new RuntimeException("租户id{" + tenantId + "}未配置");
        }
        return db;
    }
}
